package com.ronri.ams;

import android.Manifest;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cz.nongyetongv5.R;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.PermissionListener;
import com.zhy.autolayout.AutoLayoutActivity;

import java.util.List;

public class MainActivity extends AutoLayoutActivity
        implements View.OnClickListener {

    private static final int REQUEST_CODE_PERMISSION_SD = 100;
    private static final int REQUEST_CODE_PERMISSION_OTHER = 101;
    private static final int REQUEST_CODE_SETTING = 300;

    private framentMain fg_main;
    private framentUser fg_user;
    private framentMost fg_most;
    private framentAbout fg_about;

    // 定义每个选项中的相关控件
    private RelativeLayout mainLayout;
    private RelativeLayout userLayout;
    private RelativeLayout mostLayout;
    private RelativeLayout aboutLayout;

    // 工具条样式控制
    private ImageView[] ToolImage = new ImageView[4];
    private TextView[] ToolText = new TextView[4];

    private TextView mTitle = null;
    // 模块管理
    private FragmentManager fragmentManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_fragment);


        // 申请多个权限。
        AndPermission.with(this)
                .requestCode(REQUEST_CODE_PERMISSION_OTHER)
                .permission(Manifest.permission.GET_ACCOUNTS,
                        Manifest.permission.READ_CONTACTS,
                        Manifest.permission.CAMERA,
                        Manifest.permission.RECORD_AUDIO,
                        Manifest.permission.ACCESS_COARSE_LOCATION,
                        Manifest.permission.ACCESS_FINE_LOCATION,
                        Manifest.permission.ACCESS_WIFI_STATE,
                        Manifest.permission.ACCESS_NETWORK_STATE,
                        Manifest.permission.CHANGE_WIFI_STATE,
                        Manifest.permission.READ_PHONE_STATE,
                        Manifest.permission.INTERNET,
                        Manifest.permission.VIBRATE,
                        Manifest.permission.WAKE_LOCK,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        Manifest.permission.READ_EXTERNAL_STORAGE)
                .callback(permissionListener)
                .start();

        mTitle = (TextView)findViewById(R.id.title_text_tv);
        fragmentManager = getSupportFragmentManager();

        // 初始化视图
        initView();

        // 选择第一个页面
        setChioceItem(0);
    }

    /**
     * 初始化页面
     */
    private void initView() {
        // 初始化底部导航栏的控件
        mainLayout = (RelativeLayout) findViewById(R.id.first_layout);
        userLayout = (RelativeLayout) findViewById(R.id.second_layout);
        mostLayout = (RelativeLayout) findViewById(R.id.third_layout);
        aboutLayout = (RelativeLayout) findViewById(R.id.fourth_layout);

        mainLayout.setOnClickListener(this);
        userLayout.setOnClickListener(this);
        mostLayout.setOnClickListener(this);
        aboutLayout.setOnClickListener(this);


        ToolText[0] = (TextView) findViewById(R.id.tool_main_textview);
        ToolImage[0] = (ImageView) findViewById(R.id.tool_main_imageview);

        ToolText[1] = (TextView) findViewById(R.id.tool_user_textview);
        ToolImage[1] = (ImageView) findViewById(R.id.tool_user_imageview);

        ToolText[2] = (TextView) findViewById(R.id.tool_most_textview);
        ToolImage[2] = (ImageView) findViewById(R.id.tool_most_imageview);

        ToolText[3] = (TextView) findViewById(R.id.tool_about_textview);
        ToolImage[3] = (ImageView) findViewById(R.id.tool_about_imageview);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.first_layout:
                setChioceItem(0);
                break;
            case R.id.second_layout:
                setChioceItem(1);
                break;
            case R.id.third_layout:
                setChioceItem(2);
                break;
            case R.id.fourth_layout:
                setChioceItem(3);
                break;
            default:
                break;
        }
    }

    /**
     * 设置点击选项卡的事件处理
     *
     * @param index 选项卡的标号：0, 1, 2, 3
     */
    private void setChioceItem(int index) {
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

        // 设置动画效果
//        fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);

        // 清空, 重置选项, 隐藏所有Fragment
        clearChioce();

        // 首先隐藏所有的Fragment页面
        hideFragments(fragmentTransaction);

        switch (index) {
            case 0:
                ToolImage[0].setImageResource(R.drawable.home_hover);
                ToolText[0].setTextColor(getResources().getColor(R.color.colorToolActiveFont));
                mainLayout.setBackgroundColor(getResources().getColor(R.color.colorToolActiveBackGround));

                // 如果fg1为空，则创建一个并添加到界面上
                if (fg_main == null) {
                    fg_main = new framentMain();
                    fragmentTransaction.add(R.id.content, fg_main);
                } else {
                    // 如果不为空，则直接将它显示出来
                    fragmentTransaction.show(fg_main);
                }
                mTitle.setText(R.string.app_name);
                break;
            case 1:
                ToolImage[1].setImageResource(R.drawable.user_hover);
                ToolText[1].setTextColor(getResources().getColor(R.color.colorToolActiveFont));
                mainLayout.setBackgroundColor(getResources().getColor(R.color.colorToolActiveBackGround));

                if (fg_user == null) {
                    fg_user = new framentUser();
                    fragmentTransaction.add(R.id.content, fg_user);
                } else {
                    fragmentTransaction.show(fg_user);
                }
                mTitle.setText(R.string.app_user);
                break;
            case 2:
                ToolImage[2].setImageResource(R.drawable.more_hover);
                ToolText[2].setTextColor(getResources().getColor(R.color.colorToolActiveFont));
                mainLayout.setBackgroundColor(getResources().getColor(R.color.colorToolActiveBackGround));

                if (fg_most == null) {
                    fg_most = new framentMost();
                    fragmentTransaction.add(R.id.content, fg_most);
                } else {
                    fragmentTransaction.show(fg_most);
                }
                mTitle.setText(R.string.app_most);
                break;
            case 3:
                ToolImage[3].setImageResource(R.drawable.about_hover);
                ToolText[3].setTextColor(getResources().getColor(R.color.colorToolActiveFont));
                mainLayout.setBackgroundColor(getResources().getColor(R.color.colorToolActiveBackGround));

                if (fg_about == null) {
                    fg_about = new framentAbout();
                    fragmentTransaction.add(R.id.content, fg_about);
                } else {
                    fragmentTransaction.show(fg_about);
                }
                mTitle.setText(R.string.app_about);
                break;
        }

        fragmentTransaction.commit(); // 提交
    }

    /**
     * 当选中其中一个选项卡时，其他选项卡重置为默认
     */
    private void clearChioce() {
        ToolImage[0].setImageResource(R.drawable.home);
        ToolText[0].setTextColor(getResources().getColor(R.color.colorToolDefaultFont));
        mainLayout.setBackgroundColor(getResources().getColor(R.color.colorToolBackGround));

        ToolImage[1].setImageResource(R.drawable.user);
        ToolText[1].setTextColor(getResources().getColor(R.color.colorToolDefaultFont));
        userLayout.setBackgroundColor(getResources().getColor(R.color.colorToolBackGround));

        ToolImage[2].setImageResource(R.drawable.more);
        ToolText[2].setTextColor(getResources().getColor(R.color.colorToolDefaultFont));
        mostLayout.setBackgroundColor(getResources().getColor(R.color.colorToolBackGround));

        ToolImage[3].setImageResource(R.drawable.about);
        ToolText[3].setTextColor(getResources().getColor(R.color.colorToolDefaultFont));
        aboutLayout.setBackgroundColor(getResources().getColor(R.color.colorToolBackGround));
    }

    /**
     * 隐藏Fragment
     *
     * @param fragmentTransaction
     */
    private void hideFragments(FragmentTransaction fragmentTransaction) {
        if (fg_main != null) {
            fragmentTransaction.hide(fg_main);
        }
        if (fg_user != null) {
            fragmentTransaction.hide(fg_user);
        }
        if (fg_most != null) {
            fragmentTransaction.hide(fg_most);
        }
        if (fg_about != null) {
            fragmentTransaction.hide(fg_about);
        }
    }

    /**
     * 回调监听。
     */
    private PermissionListener permissionListener = new PermissionListener() {
        @Override
        public void onSucceed(int requestCode, List<String> grantPermissions) {
            switch (requestCode) {
                case REQUEST_CODE_PERMISSION_SD: {
                    break;
                }
                case REQUEST_CODE_PERMISSION_OTHER: {
                    Log.i("ttt", grantPermissions.get(0));
                    // Toast.makeText(activity_cover.this, R.string.message_post_succeed, Toast.LENGTH_SHORT).show();
                    break;
                }
            }
        }

        @Override
        public void onFailed(int requestCode, List<String> deniedPermissions) {
            switch (requestCode) {
                case REQUEST_CODE_PERMISSION_SD: {
                    // Toast.makeText(activity_cover.this, R.string.message_calendar_failed, Toast.LENGTH_SHORT).show();
                    break;
                }
                case REQUEST_CODE_PERMISSION_OTHER: {
                    // Toast.makeText(activity_cover.this, R.string.message_post_failed, Toast.LENGTH_SHORT).show();
                    break;
                }
            }

            // 用户否勾选了不再提示并且拒绝了权限，那么提示用户到设置中授权。
            if (AndPermission.hasAlwaysDeniedPermission(MainActivity.this, deniedPermissions)) {
                // 第一种：用默认的提示语。
                AndPermission.defaultSettingDialog(MainActivity.this, REQUEST_CODE_SETTING).show();
            }
        }
    };
}
